package org.common.utils.generatortable.event.generator;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.common.utils.field.FieldInfo;
import org.common.utils.field.GeneratorConfig;
import org.common.utils.generatortable.GeneratorTableManager;
import org.common.utils.tool.VelocityTool;
import org.common.utils.tool.window.VersionTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
 * 生成配置类
 * @author zhouzhibing
 * @date 2025/4/27
 */
public class GeneratorService {
    private static final Logger log = LoggerFactory.getLogger(GeneratorTableManager.class);


    static {
        VelocityTool.init();
    }

    public static final GeneratorService INSTANCE = new GeneratorService();

    private GeneratorService() {

    }

    public static GeneratorService getInstance() {
        return INSTANCE;
    }

    /**
     * 执行生成配置类
     * @param config 生成配置
     */
    public void generator(GeneratorConfig config) {
        String allPath = config.getAllPath();

        checkDir(allPath);

        generatorAbstract(config);
        generatorData(config);
        generatorEntity(config);
    }

    /**
     * 生成配置类
     * @param config 生成配置
     */
    private void generatorData(GeneratorConfig config) {
        String packageName = config.getPackageName();
        String className = config.getClassName();
        List<FieldInfo> fieldList = config.getFieldList();

        String fileName = className + "Data.java";
        String filePath = config.getAllPath() + fileName;
        File dataFile = new File(filePath);
        if(dataFile.exists()) {
            log.info("文件已存在，不进行覆盖生成！ filePath = {} " , filePath);
            return;
        }

        VelocityContext context = new VelocityContext();
        context.put("fileName", fileName);
        context.put("className", className);
        context.put("packageName", packageName);
        context.put("fieldList", fieldList);

        try (FileWriter writer = new FileWriter(filePath)) {
            Template template = Velocity.getTemplate("velocity/data.vm", "UTF-8");
            template.merge(context , writer);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 生成实体类
     * @param config 生成配置
     */
    private void generatorEntity(GeneratorConfig config) {
        String packageName = config.getPackageName();
        String className = config.getClassName();
        List<FieldInfo> fieldList = config.getFieldList();

        String fileName = className + ".java";
        String filePath = config.getAllPath() + fileName;
        File dataFile = new File(filePath);
        if(dataFile.exists()) {
            log.info("文件已存在，不进生覆盖生成！ filePath = {} " , filePath);
            return;
        }

        VelocityContext context = new VelocityContext();
        context.put("fileName", fileName);
        context.put("className", className);
        context.put("packageName", packageName);
        context.put("fieldList", fieldList);

        try (FileWriter writer = new FileWriter(filePath)) {
            Template template = Velocity.getTemplate("velocity/entity.vm", "UTF-8");
            template.merge(context , writer);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 生成抽象类
     * @param config 生成配置
     */
    private void generatorAbstract(GeneratorConfig config) {
        boolean isKvConfig = config.isKvConfig();
        String packageName = config.getPackageName();
        String className = config.getClassName();
        List<FieldInfo> fieldList = config.getFieldList();

        String fileName = "Abstract" + className + ".java";
        String filePath = config.getAllPath() + fileName;

        VelocityContext context = new VelocityContext();
        context.put("isKvConfig", isKvConfig);
        context.put("fileName", fileName);
        context.put("className", className);
        context.put("packageName", packageName);
        context.put("fieldList", fieldList);

        try (FileWriter writer = new FileWriter(filePath)) {
            Template template = Velocity.getTemplate("velocity/abstract.vm", "UTF-8");
            template.merge(context , writer);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 检查目录，并生成目录
     * @param allPath 全路径
     */
    private void checkDir(String allPath) {
        File generatorDirFile = new File(allPath);
        if(!generatorDirFile.exists()) {
            generatorDirFile.mkdirs();
        }
    }

}
